/**    
 * 文件名：${module}Controller.java
 * 版本信息:V1.00  
 * 日期：${createdTime} 
 * Copyright 深圳市佰仟金融服务有限公司版权所有
 */ 
package com.billionsfinance.crs.controller.${moduleType};

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.billionsfinance.common.framework.RemoteCall;
import com.billionsfinance.crs.model.PageResult;
import com.billionsfinance.crs.model.system.MessageModel;
import com.billionsfinance.crs.model.${moduleType}.${module};
import com.billionsfinance.crs.request.PageReuqest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;
import com.billionsfinance.crs.server.${moduleType}.I${module}Server;
import com.billionsfinance.crs.controller.BaseController;

 /**
* ${hessianDesc}Controller
* @FileName:${module}Controller.java
* @author:${createdName}
* @date:${createdTime}
* @Version:V1.00
*/
@Controller
@RequestMapping("/${moduleType}/${lowModule}")
public class ${module}Controller extends BaseController{
	
	/**
	 * 进入${module}服务列表页面
	 * @param request 请求参数
	 * @param response 返回参数
	 * @param view 跳转的视图
	 * @return  String 视图
	 * @throws Exception 异常
	 */
	@RequestMapping("/index/{view}")
	public String index(HttpServletRequest request, HttpServletResponse response, @PathVariable("view") String view) throws Exception {
		LOGGER.info("---------------进入${module}Controller.index()-------------------");
	
		return "/${moduleType}/${lowModule}/"+view;
	}
	/**
	 * 根据ID读取${module}信息
	 * @param modelAndView  
	 * @param eid ${module}ID
	 * @param request 请求参数
	 * @return ModelAndView  
	 */
	@RequestMapping(value="/showView/{id}", method=RequestMethod.GET)
	public ModelAndView  showView(ModelAndView modelAndView, @PathVariable("id") String eid, HttpServletRequest request){
		try{
			Long id=Long.valueOf(eid);
			I${module}Server ${lowModule}Server = getServer(I${module}Server.class);
			${module} ${lowModule}= ${lowModule}Server.read${module}ById(id);
			modelAndView.addObject("${lowModule}",  ${lowModule});
			modelAndView.setViewName("/${moduleType}/${lowModule}/edit");
			
		}catch(Exception e){
		  LOGGER.error("${module}Controller.showView() Error:" + e.getMessage(), e);
		}
		return modelAndView;
	}
	
	
	/**
	 * ${module}列表页面查询
	 * @param request 请求参数
	 * @param response 返回参数
	 * @param pageReuqest 分页参数 
	 * @return Object 返回JSON
	 * @throws Exception 异常
	 */
	@RequestMapping(value="/query", method=RequestMethod.POST)
	@ResponseBody
	public MessageModel queryList(HttpServletRequest request, HttpServletResponse response, PageReuqest pageReuqest)  {
		LOGGER.info("---------------进入${module}Controller.queryList()-------------------");
		MessageModel messageModel = new MessageModel(true);
		try {
			I${module}Server ${lowModule}Server = RemoteCall.getRemoteObject(I${module}Server.class);
			//hessian接口调用查询
			PageResult<${module}> result = ${lowModule}Server.queryPageList(new ${module}(), pageReuqest);
			messageModel.setResult(result);
		    Map<String, Object> map = new HashMap<String, Object>();
			map.put("total", result.getPage().getTotalSize());
			map.put("rows", result.getResult());
			messageModel.setResult(map);
		} catch (Exception e) {
			messageModel.setSuccess(false);
			messageModel.setMsg("查询失败");
			LOGGER.error("${module}Controller.queryList() Error:" + e.getMessage(), e);
		}
		LOGGER.info("---------------退出${module}Controller.queryList()-------------------");
		return messageModel;
	}
	/**
	 * 新增或修改${module}
	 * @param request 请求参数
	 * @param response 输出参数
	 * @param ${lowModule} 
	 * @return Object 返回JSON对象 
	 * @throws Exception 
	 */
	@RequestMapping("/save")
	@ResponseBody
	public MessageModel add(HttpServletRequest request, HttpServletResponse response, ${module} ${lowModule}) {
		LOGGER.info("---------------进入${module}Controller.add()-------------------");
		MessageModel messageModel = new MessageModel();
		try {
			I${module}Server ${lowModule}Server = RemoteCall.getRemoteObject(I${module}Server.class);
			//添加数据
			int rows = ${lowModule}Server.createOrUpdate${module}(${lowModule});
		    if (rows == 1) {
				messageModel.setSuccess(true);
				messageModel.setMsg("添加成功！");
			} else {
				messageModel.setSuccess(false);
				messageModel.setMsg("添加失败！");
			}
		} catch (Exception e) {
			messageModel.setSuccess(false);
			messageModel.setMsg("查询失败");
			LOGGER.error("${module}Controller.add() Error:" + e.getMessage(), e);
		}
		LOGGER.info("---------------退出${module}Controller.add()-------------------");
		return messageModel;
	}
	/**
	 * 删除${module}
	 * @param request 请求参数
	 * @param response 输出参数
	 * @param eid ${module}Id 
	 * @return Object 返回JSON 
	 */
	@RequestMapping("/del/{id}")
	@ResponseBody
	public MessageModel delete(HttpServletRequest request, @PathVariable("id") String eid, HttpServletResponse response) {
		LOGGER.info("---------------进入${module}Controller.delete()-------------------");
		//返回结果
		MessageModel messageModel = new MessageModel();
		Long id=Long.valueOf(eid);
		try {
			I${module}Server ${lowModule}Server = RemoteCall.getRemoteObject(I${module}Server.class);
			//删除数据
			int rows = ${lowModule}Server.delete${module}ById(id);
			if (rows == 1) {
				messageModel.setSuccess(true);
				messageModel.setMsg("删除成功！");
			} else {
				messageModel.setSuccess(false);
				messageModel.setMsg("删除失败！");
			}
		} catch (Exception e) {
		    messageModel.setSuccess(false);
			messageModel.setMsg("查询失败");
			LOGGER.error("${module}Controller.delete() Error:" + e.getMessage(), e);
		}
		LOGGER.info("---------------退出${module}Controller.delete()-------------------");
		return messageModel;
	}
	
}
